<template>
  <div class="modal">
    <div class="mask" @click="changeisShowModal(false)"></div>
    <div class="loginbox">
      <div class="close" @click="changeisShowModal(false)"></div>
      <section class="nav">
        <a
          href="javascript:;"
          @click="isShowPhone = true"
          :class="{ active: isShowPhone }"
          >手机号登录</a
        >
        <span> | </span>
        <a href="javascript:;" @click="showQr" :class="{ active: !isShowPhone }"
          >微信扫码登录</a
        >
      </section>
      <div class="content">
        <div class="phonebox" v-show="isShowPhone">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="imgArr"
            ></slide-verify>
          </div>
          <div class="row">
            <input type="text" placeholder="请输入验证码" v-model="code" />
            <button class="btncode" v-show="isShowBtnCode" @click="getCode">
              获取验证码
            </button>
            <button class="btncode" v-show="!isShowBtnCode">{{ count }}</button>
          </div>
          <div class="row">
            <button class="btnlogin" @click="submitFn">登录</button>
          </div>
        </div>
        <div class="qrbox" v-show="!isShowPhone" id="weixin">二维码</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { sendMSMAPI, PhoneReginAPI, BindingAPI } from "../request/api";
import { validatePhoeNumber } from "@/utlis";
export default {
  data() {
    return {
      msg: "向右滑动",
      imgArr: [
        require("../assets/img/bb1.png"),
        require("../assets/img/bb2.png"),
        require("../assets/img/bb3.png"),
      ],
      isShowBtnCode: true,
      isShowPhone: true,
      count: 10,
      phoneNumber: "",
      code: "",
    };
  },
  methods: {
    ...mapMutations({
      changeisShowModal: "showModal/changeisShowModal",
      changeIsShowLoginModal: "loginModal/changeIsShowLoginModal",
    }),
    ...mapActions({
      asyncchangeIsShowToast: "showToast/asyncchangeIsShowToast",
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    async getCode() {
      if (!this.toVerify()) return;
      let res = await sendMSMAPI({
        phone: this.phoneNumber,
      });
      if (!res) return;
      if (res.code == 0) {
        this.isShowBtnCode = false;
        setInterval(() => {
          this.count--;
          if (this.count == 0) {
            this.isShowBtnCode = true;
            this.count = 10;
          }
        }, 1000);
      }
    },
    toVerify() {
      if (!validatePhoeNumber(this.phoneNumber)) {
        // alert("请输入正确的手机号");
        this.asyncchangeIsShowToast({
          isshow: true,
          type: "danger",
          msg: "请输入正确的手机号",
        });
        return;
      }
      if (this.msg == "向右滑动" || this.msg == "再试一次") {
        // alert("验证失败或者未验证，请先登录");
        this.asyncchangeIsShowToast({
          isshow: true,
          type: "danger",
          msg: "验证失败或者未验证，请先登录",
        });
        return;
      }
      return true;
    },
    async submitFn() {
      let uuid = sessionStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        res = await BindingAPI({
          phone: this.phoneNumber,
          verifyCode: this.code,
          uuid,
        });
      } else {
        if (!this.toVerify()) return;
        if (this.code == "") return;
        res = await PhoneReginAPI({
          phone: this.phoneNumber,
          verifyCode: this.code,
        });
      }
      sessionStorage.removeItem("uuid");
      sessionStorage.setItem("x-auth-token", res["x-auth-token"]);
      // alert("登录成功");
      this.asyncchangeIsShowToast({
        isshow: true,
        type: "success",
        msg: "登录成功",
      });
      this.changeIsShowLoginModal(true);
      this.changeisShowModal(false);

      this.asyncchangeUserInfo();
    },
    showQr() {
      this.isShowPhone = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
  },
};
</script>
 
<style lang = "less" scoped>
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .loginbox {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    background: url("../assets/img/login-box-bg.png");
    width: 555px;
    height: 423px;
    .close {
      position: absolute;
      top: 15px;
      right: 60px;
      background: url("../assets/img/close.png");
      width: 22px;
      height: 22px;
      cursor: pointer;
    }
    .nav {
      text-align: center;
      padding-top: 50px;
      font-size: 18px;
      a {
        text-decoration: none;
        color: black;
        &.active {
          color: paleturquoise;
        }
      }
    }
    .content {
      width: 355px;
      margin: 10px auto 0;
      .phonebox {
        margin-top: 20px;
        .row {
          display: flex;
          margin-bottom: 20px;
          input {
            flex: 1;
            height: 50px;
            text-indent: 1em;
            outline: none;
          }
          button {
            height: 50px;
            background-color: #0a328e;
            color: #fff;
            border: none;
          }
          .btncode {
            margin-left: 10px;
            height: 55px;
            width: 86px;
          }
          .btnlogin {
            width: 100%;
          }
        }
      }
    }
  }
}
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
#weixin {
  text-align: center;
}
</style>